package cn.wolfcode.luowowo.redis.redisService.impl;

import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.key.RedisKeys;
import cn.wolfcode.luowowo.redis.redisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.util.Consts;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.Key;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void setVerifyCode(String phone, String code) {
        //设计key
        String key = RedisKeys.VERIFY_CODE.join(phone);//使用枚举工具类拼接
        //参数1: key , 参数2: value , 参数3: 有效时间长度 , 参数4: 时间单位
        redisTemplate.opsForValue().set(key, code, RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        //根据电话号码在缓存中获取对应的验证码
        String key = RedisKeys.VERIFY_CODE.join(phone);//使用枚举工具类拼接
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setToken(String token, UserInfo userInfo) {
        String key = RedisKeys.USER_LOGIN_TOKEN.join(token);
        String value = JSON.toJSONString(userInfo);
        //将用户信息设置到redis中  设置有效时间为30分钟
        redisTemplate.opsForValue().set(key, value,
                RedisKeys.USER_LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public UserInfo getUserByToken(String token) {
        if (!StringUtils.hasLength(token)) {
            return null;
        }
        String key = RedisKeys.USER_LOGIN_TOKEN.join(token);//根据令牌拼接key
        if (redisTemplate.hasKey(key)) {
            //延长key的 有效时间
            redisTemplate.expire(key, RedisKeys.USER_LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
            //获取用户对象
            String user = redisTemplate.opsForValue().get(key);
            //JSON字符串 转成 对象
            return JSON.parseObject(user, UserInfo.class);
        }
        return null;
    }

    @Override
    public void setCounter() {
        String key = RedisKeys.COUNTER.join(RedisKeys.VERIFY_CODE.getPrefix());
        String value = JSON.toJSONString(Consts.VERIFY_CODE_VAI_NUMBER);
        redisTemplate.opsForValue().set(key, value, RedisKeys.COUNTER.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getCounter() {
        String key = RedisKeys.COUNTER.join(RedisKeys.VERIFY_CODE.getPrefix());
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void CounterDecr() {
        String key = RedisKeys.COUNTER.join(RedisKeys.VERIFY_CODE.getPrefix());
        redisTemplate.opsForValue().decrement(key, 1);
    }
}


